AI Verified

Name

Automatically Register Guest Customers WooCommerce Checkout

About

Automatically Register Guest Customers and Programmatically Log Them In

Language

PHP

Rating

Voted: 1 by 1 user(s)

How to Setup Snippet

Please make sure this is legal in your country and also that your checkout visitors are aware they will become registered customers without explicit consent (i.e. without ticking a “create an account on this site” checkbox). So, , there is a way to turn guest checkouts into registered customer ones. Also, there is a neat WooCommerce function to bulk add all past guest orders to a new customer (wc_update_new_customer_past_orders). Of course, “Allow customers to place orders without an account” must be enabled in your WooCommerce settings, otherwise you’re not allowing guest checkouts and the snippet will be irrelevant.

Codevault

George Snippet Library

Scroll down to see more snippets from this codevault.

Wordpress Compatability

The author has indicated that this snippet is compatable up to wordpress version: Not Specified

Our AI bot has checked this snippet is compatable up to wordpress version: 6.1

Code Snippet Plugin Sync

Free & Pro

Download this snippet by clicking the download button, then head over to the Code Snippet Plugin settings in your wordpress admin dashboard, select the import menu then upload this file to import into your wordpress site.

Pro Only (Coming Soon)

You will be able to click a button and sync this snippet to your wordpress site automatically and from your dashboard manage all code snippets across all your wordpress sites that have the Code Snippets Pro plugin installed.

History

Last modified:

20/04/2023

Important Note

This snippet has the following status:

AI Verified

This snippet has been tested by our AI bot, see any comments below.

AI Bot Comments:

-------------------------------------------------------------------------------------------------------------
Name | Potential vulnerability found : Insecure E-mail
Line | -1
Code | mail() && ! username_exists( $email )
Code | Line | 9 : $email = $order->get_billing_email());

Found 1 vulnerabilities

Automatically Register Guest Customers WooCommerce Checkout

 
                    
1/*
2 *
3 * Automatically Register Guest Customers @ WooCommerce Checkout (and Programmatically Log Them In @ WooCommerce Thank You Page
4 * */
5add_action( 'woocommerce_thankyou', 'gncy_register_guests', 9999 );
6 
7function gncy_register_guests( $order_id ) {
8 $order = wc_get_order( $order_id );
9 $email = $order->get_billing_email();
10 if ( ! email_exists( $email ) && ! username_exists( $email ) ) {
11 $customer_id = wc_create_new_customer( $email, '', '', array(
12 'first_name' => $order->get_billing_first_name(),
13 'last_name' => $order->get_billing_last_name(),
14 ));
15 if ( is_wp_error( $customer_id ) ) {
16 throw new Exception( $customer_id->get_error_message() );
17 }
18 wc_update_new_customer_past_orders( $customer_id );
19 wc_set_customer_auth_cookie( $customer_id );
20 } else {
21 $user = get_user_by( 'email', $email );
22 wc_update_new_customer_past_orders( $user->ID );
23 wc_set_customer_auth_cookie( $user->ID );
24 }
25}

1

Related Snippets

Please see some snippets below related to this snippet..

WooCommerce

AI Verified

1

Hide Other Shipping Methods When Free Shipping Available

Added: 1 year ago

Last Updated: 1 year ago

Hide Other Shipping Methods When Free Shipping Available

WooCommerce

AI Verified

1

WooCommerce Show Categories On Product Loop

Added: 1 year ago

Last Updated: 1 day ago

Being able to show which categories a product belongs to on the archive page make it clear to users that a product may belong to more than one category and also that there are more categories to navig...

WooCommerce

AI Verified

1

WooCommerce Show Custom Text Above Login Form Fields

Added: 1 year ago

Last Updated: 1 day ago

This guide shows you how to add custom content above the login form fields shown on the My Account page.

Other Snippets in this Codevault

These are some popular snippets from this users codevault..

WooCommerce

AI Verified

5

Delete Woocommerce images after deleting product

Added: 1 year ago

Last Updated: 3 weeks ago

Automatically Delete WooCommerce Images After Deleting a Product. If you use the same image of multiple products the image will not be deleted for those

WooCommerce

AI Verified

2

Add Alphabetical Sorting to Woocommerce

Added: 1 year ago

Last Updated: 1 day ago

Sorts products alphabetically in Woocommerce

WooCommerce

AI Verified

1

Remove categories from the shop and other pages in Woocommerce

Added: 1 year ago

Last Updated: 1 year ago

Remove any categories from WooCommerce frontend